<template>
  <a-modal
    title="新增采集设备"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          label="所属站点"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
          :hidden="true"
        >
          <a-select style="width: 100%" placeholder="请选择所属站点" v-decorator="['stationId', {rules: [{ required: true, message: '请选择所属站点！' }]}]" >
            <a-select-option v-for="(item,index) in stationTree" :key="index" :value="item.id" >{{ item.stationName }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="网关名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入网关名称" v-decorator="['gatewayName', {rules: [{required: true, message: '请输入网关名称！'}]}]" />
        </a-form-item>

        <a-form-item
          label="网关编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入网关编号" v-decorator="['gatewayCode', {rules: [{required: true, message: '请输入网关编号！'}]}]" />
        </a-form-item>

        <a-form-item
          label="网关设备IP"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
          hidden
        >
          <a-input placeholder="请输入网关设备IP" v-decorator="['gatewayIp']" />
        </a-form-item>

        <a-form-item
          label="网关状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          style="text-align: left"
        >
          <a-radio-group v-decorator="['status',{ initialValue: 1 ,rules: [{ required: true, message: '请选择网关状态！' }]}]" >
            <a-radio :value="1">启用</a-radio>
            <a-radio :value="0">禁用</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          label="声卡类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select style="width: 100%" placeholder="请选择声卡类型"
                    v-decorator="['soundCardId', {rules: [{ required: true, message: '请选择声卡！' }]}]">
            <a-select-option v-for="(item,index) in soundCardList" :key="index" :value="item.id">{{ item.cardName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="网关类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select style="width: 100%" placeholder="请选择网关类型" v-decorator="['typeCode', {rules: [{ required: true, message: '请选择网关类型！' }]}]" >
            <a-select-option v-for="(item,index) in gatewayTypeList" :key="index" :value="item.code" >{{ item.modelName }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="网关运行状态"
          hidden
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          style="text-align: left"
        >
          <a-radio-group v-decorator="['onlineStatus',{ initialValue: 1 ,rules: [{ required: true, message: '请选择网关运行状态！' }]}]" >
            <a-radio :value="1">在线</a-radio>
            <a-radio :value="0">离线</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          label="网关配置场景"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select style="width: 100%" placeholder="请选择网关配置场景" v-decorator="['sceneId', {rules: [{ required: true, message: '请选择网关配置场景' }]}]" >
            <a-select-option v-for="(item,index) in sceneList" :key="index" :value="item.id" >{{ item.sceneName }}</a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { add } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { list } from '@/api/modular/main/bizstation/bizStationManage'
  import { gatewayTypelist } from '@/api/modular/main/bizgatewaytype/bizGatewayTypeManage'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
  import { listScene } from '@/api/modular/main/gatewayscene/gatewayscene'
  import { listSoundCard } from '@/api/modular/main/soundCard'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        stationTree: [],
        gatewayTypeList: [],
        treeLoading: true,
        form: this.$form.createForm(this),
        birthdayString: [],
        groupCodeList: [],
        sceneList: [],
        soundCardList: []
      }
    },

    created () {
      this.getSoundCardList()
    },

    methods: {
      getSoundCardList () {
        listSoundCard().then(res => {
          if (res.success) {
            this.soundCardList = res.data
          }
        })
      },
      add (record) {
        const that = this
        that.visible = true
        setTimeout(() => {
          that.form.setFieldsValue({
            stationId: record
          })
        }, 100)
        that.getStationTree()
        that.sysDictTypeDropDown()
        that.getSceneList()
      },
      getStationTree () {
        list().then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.stationTree = res.data
        })
        gatewayTypelist().then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.gatewayTypeList = res.data
        })
      },
      getSceneList () {
        listScene().then(res => {
          this.sceneList = res.data
        })
      },
      sysDictTypeDropDown () {
        sysDictTypeDropDown({ code: 'gateway_config_scene' }).then((res) => {
          if (res.success) {
            this.groupCodeList = res.data
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            // 默认网关运行状态为在线
            add(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('新增成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('新增失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },

      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
